﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using RVNetworkLib;

namespace QueryRooms
{
    public partial class Form1 : Form
    {
        private LanClient _client;
        private List<LanRoomInfo> _rooms;

        public Form1()
        {
            InitializeComponent();

            _client = new LanClient();
            _rooms = new List<LanRoomInfo>();
        }

        private void _btnRefresh_Click(object sender, EventArgs e)
        {
            _btnRefresh.Enabled = false;
            _listRooms.Enabled = false;
            _listRooms.Items.Clear();
            _rooms.Clear();

            RDP rdp = new RDP(3000);

            Thread searchWorker = new Thread(() =>
                {
                    _rooms = rdp.QueryRoom();

                    _listRooms.Invoke(new Action(() =>
                        {
                            _listRooms.Items.Clear();

                            // update list
                            foreach (LanRoomInfo room in _rooms)
                            {
                                string text = String.Format("{0} - {1} [{2}, {3}] ({4}/{5})",
                                    room.Detail.MapName, room.Detail.RoomName,
                                    room.IP, room.Port,
                                    room.Detail.NumUser, room.Detail.MaxUser);
                                _listRooms.Items.Add(text);
                            }
                        }));

                    rdp.Close();
                    rdp = null;

                    _btnRefresh.Invoke(new Action(() => _btnRefresh.Enabled = true));

                    if (_rooms.Count > 0)
                    {
                        _listRooms.Invoke(new Action(() =>
                            {
                                _listRooms.Enabled = true;
                                _listRooms.SelectedIndex = 0;
                            }));
                    }
                });

            searchWorker.Start();
        }

        private void AppendLog(string msg)
        {
            _listMsg.Items.Add(msg);
        }

        private void _btnClear_Click(object sender, EventArgs e)
        {
            _listMsg.Items.Clear();
        }

        private void OnJoin(object sender, EventArgs e)
        {
            if (_listRooms.SelectedIndex != -1)
            {
                LanRoomInfo lri = _rooms[_listRooms.SelectedIndex];
                ErrorCode err = _client.Setup(lri.IP, (int)lri.Port);

                System.Diagnostics.Trace.WriteLine(String.Format("Setup client[{0}:{1}] : {2}",
                    lri.IP, lri.Port, err));

                if (err != ErrorCode.Success) return;

                err = _client.LanJoinRoom("NoName");
                System.Diagnostics.Trace.WriteLine(String.Format("Join room : {0}", err));
            }
        }
    }
}
